package com.zerosky.crm.util;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;  
/**
 * 短信通知工具类
 */
public class SMSUtil {  
    static String secret="b0d0efe85424418949746a67e7fd50e7";  
    static String appKey="24659957";  
    //正式
    static String url="http://gw.api.taobao.com/router/rest";  
    //测试
    //static String url="http://gw.api.tbsandbox.com/router/rest";  
    static String FreeSignName = "掌瑰";
    
    /**
     * 
     * @param phone 手机号
     * @param code 验证码
     * @param TemplateCode 模板ID
     * @return
     */
    public static boolean sendSMS(String phone,String code,String TemplateCode)
    {
    	TaobaoClient client = new DefaultTaobaoClient(url, appKey, secret);
    	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
    	req.setExtend("");
    	req.setSmsType("normal");
    	req.setSmsFreeSignName(FreeSignName);
    	req.setSmsParamString("{\"number\":\""+code+"\"}");
    	req.setRecNum(phone);
    	req.setSmsTemplateCode(TemplateCode);
    	AlibabaAliqinFcSmsNumSendResponse rsp;
		try {
			rsp = client.execute(req);
			if (rsp.getBody().indexOf("success")>-1) {
				return true;
			}else
			{
				System.out.println("短信发送失败:"+rsp.getBody());
				return false;
			}
		} catch (ApiException e) {
			System.out.println("短信发送失败");
			e.printStackTrace();
			return false;
		}
    }
    
    /**
     * 密码找回
     * 验证码：${code} , 您正在进行找回密码操作，切勿将验证码泄露于他人，30分钟内有效。
     * @param phone
     * @param code
     * @return
     */
    public static boolean sendSMSBackPwd(String phone,String code)
    {
    	TaobaoClient client = new DefaultTaobaoClient(url, appKey, secret);
    	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
    	req.setExtend("");
    	req.setSmsType("normal");
    	req.setSmsFreeSignName(FreeSignName);
    	req.setSmsParamString("{\"code\":\""+code+"\"}");
    	req.setRecNum(phone);
    	req.setSmsTemplateCode("SMS_105815173");
    	AlibabaAliqinFcSmsNumSendResponse rsp;
		try {
			rsp = client.execute(req);
			if (rsp.getBody().indexOf("success")>-1) {
				return true;
			}else
			{
				System.out.println("短信发送失败:"+rsp.getBody());
				return false;
			}
		} catch (ApiException e) {
			System.out.println("短信发送失败");
			e.printStackTrace();
			return false;
		}
    }
    
    /**
     * 注册
     * 您在掌瑰的验证码是 ${code}，30分钟内有效。如非本人操作，请忽略本条短信。
     * @param phone
     * @param code
     * @return
     */
    public static boolean sendSMSRegister(String phone,String code)
    {
    	TaobaoClient client = new DefaultTaobaoClient(url, appKey, secret);
    	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
    	req.setExtend("");
    	req.setSmsType("normal");
    	req.setSmsFreeSignName(FreeSignName);
    	req.setSmsParamString("{\"code\":\""+code+"\"}");
    	req.setRecNum(phone);
    	req.setSmsTemplateCode("SMS_105810142");
    	AlibabaAliqinFcSmsNumSendResponse rsp;
		try {
			rsp = client.execute(req);
			if (rsp.getBody().indexOf("success")>-1) {
				return true;
			}else
			{
				System.out.println("短信发送失败:"+rsp.getBody());
				return false;
			}
		} catch (ApiException e) {
			System.out.println("短信发送失败");
			e.printStackTrace();
			return false;
		}
    }
    
    /**
     * 发送预定信息
     * 尊敬的${name},您预定的${msg}
     * @param phone
     * @param msg
     * @param name
     * @return
     */
    public static boolean sendSMSRegister(String phone,String msg,String name)
    {
    	TaobaoClient client = new DefaultTaobaoClient(url, appKey, secret);
    	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
    	req.setExtend("");
    	req.setSmsType("normal");
    	req.setSmsFreeSignName(FreeSignName);
    	req.setSmsParamString("{\"name\":\""+name+"\",\"msg\":\""+msg+"\"}");
    	req.setRecNum(phone);
    	req.setSmsTemplateCode("SMS_105770163");
    	AlibabaAliqinFcSmsNumSendResponse rsp;
		try {
			rsp = client.execute(req);
			if (rsp.getBody().indexOf("success")>-1) {
				return true;
			}else
			{
				System.out.println("短信发送失败:"+rsp.getBody());
				return false;
			}
		} catch (ApiException e) {
			System.out.println("短信发送失败");
			e.printStackTrace();
			return false;
		}
    }
    
    
    
    
    //测试方法
    public static void main(String[] args){
    	
    	
    	TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "24659957",
				"b0d0efe85424418949746a67e7fd50e7");
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		String str="{\"number\":\""+(int)((Math.random()*9+1)*100000)+"\"}";
		System.out.println(str);
		req.setExtend("");
		req.setSmsType("normal");
		req.setSmsFreeSignName("掌瑰");
		req.setSmsParamString(str);
		req.setRecNum("18975745953");
		req.setSmsTemplateCode("SMS_105290052");
		AlibabaAliqinFcSmsNumSendResponse rsp;
		try {
			rsp = client.execute(req);
	    	System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
		
        System.out.println("success");
    }
}  
